package com.zhou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhou.dto.Answer;
import com.zhou.entity.BlanQues;
import com.zhou.mapper.BlanQuesMapper;
import com.zhou.service.BlanQuesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2023-03-24
 */
@Service
public class BlanQuesServiceImpl extends ServiceImpl<BlanQuesMapper, BlanQues> implements BlanQuesService {

    @Autowired
    private BlanQuesMapper blanQuesMapper;

    @Override
    public long getCountByQuesId(Integer quesId) {
        return blanQuesMapper.selectCount(
                new QueryWrapper<BlanQues>().eq("ques_id", quesId));
    }

    @Override
    public List<Integer> checkAllAnswers(Integer quesId, List<Answer> answers) {
        return answers.stream().map(answer -> {
            BlanQues blanQues = blanQuesMapper.selectOne(
                    new QueryWrapper<BlanQues>().eq("ques_id", quesId).eq("sub_id", answer.getSubId()));
            if (blanQues != null) {
                System.out.println("Database answer: [" + blanQues.getAnswer() + "]");
                System.out.println("Submitted answer: [" + answer.getAnswer() + "]");
                if (blanQues.getAnswer().equalsIgnoreCase(answer.getAnswer())) {
                    return 1;
                }
            }
            return 0;
        }).collect(Collectors.toList());
    }
}
